package org.feiye.studyday28.two;

import java.io.*;
import java.net.*;
import java.util.*;

public class ServerThread extends Thread {
	private Socket s;
	private User u;

	List<User> l = new LinkedList<>();

	public ServerThread(Socket s) {
		super();
		this.s = s;
		// 初始化用户对象
		User u1 = new User("abcd", "1234");
		User u2 = new User("efgh", "4567");
		User u3 = new User("igkl", "1122");
		User u4 = new User("mnop", "1133");
		User u5 = new User("qrst", "1144");
		l.add(u1);
		l.add(u2);
		l.add(u3);
		l.add(u4);
		l.add(u5);
	}

	@Override
	public void run() {
		try {
			// 打开对象输入输出流
			InputStream in = s.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(in);
			OutputStream os = s.getOutputStream();
			PrintWriter pw = new PrintWriter(os);
			while (true) {
				User u0 = (User) ois.readObject();
				for (int i = 0; i < l.size(); i++) {
					if (l.get(i).getAct().equals(u0.getAct()) && l.get(i).getPsd().equals(u0.getPsd())) {
						String str = "服务器说：恭喜您登录成功！";
						pw.println(str);
						pw.flush();
						System.out.println(str);
						break;
					} else if (i == l.size() - 1) {
						String str = "服务器说：账号或密码错误";
						System.out.println(str);
						pw.println(str);
						pw.flush();
					}

				}
			}
		} catch (Exception e) {
		} finally {
			if (s != null) {
				try {
					s.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
